package com.eerl.pdev.util;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.awt.RelativePoint;

import javax.swing.*;
import java.util.Objects;

public class Notice {

    private static final String NOTICE_GROUP = "EToolsNotificationGroup";

    /**
     * 弹出消息提示
     */
    public static void popMessage(AnActionEvent event, String message) {
        JBPopupFactory.getInstance()
                .createHtmlTextBalloonBuilder(message, null, com.intellij.ui.JBColor.YELLOW, null)
                .setFadeoutTime(3000)
                .createBalloon()
                .show(RelativePoint.getSouthEastOf(getJComponentFromEvent(event)), Balloon.Position.above);
    }
    private static JComponent getJComponentFromEvent(AnActionEvent event) {
        if (event.getInputEvent().getComponent() instanceof JComponent) {
            return (JComponent) event.getInputEvent().getComponent();
        } else {
            // Attempt to find a suitable JComponent in the project window
            Project project = event.getProject();
            if (project != null) {
                JComponent component = Objects.requireNonNull(WindowManager.getInstance().getIdeFrame(project)).getComponent();
                if (component != null) {
                    return component;
                }
            }
        }
        throw new NullPointerException();
    }

    /**
     * 这是一个消息，能显示在通知栏
     */
    public static void showNotification(AnActionEvent event, String content, NotificationType type) {
        showNotification(event.getProject(), content, type);
    }
    public static void showNotification(Project project, String content, NotificationType type) {
        NotificationGroup notificationGroup = NotificationGroupManager.getInstance().getNotificationGroup(NOTICE_GROUP);
        Notification notification = notificationGroup.createNotification(content, type);
        notification.notify(project);
    }

    /**
     * 提示框，居中弹脸
     */
    public static void showDialogMessage(String title, String message) {
        Messages.showMessageDialog(message, title, Messages.getInformationIcon());
    }
}
